import { productApi } from "@/api/erp/drug/drug";
import { Button, message } from "antd";
import { useTranslation } from "react-i18next";
import "./index.less";
import { useEffect, useState } from "react";
import { formatDate } from "@/utils/util";
import { BasicProTable, useProTable } from "@/components/ViewComponents/proTable";

const index = ({ eqpId, setPageFn }: any) => {
	const { t } = useTranslation();
	const en_vi = "drugStorage.equipment.device";
	const [dataSource, setDataSource] = useState([]);
	const defaultColumns = [
		{
			title: t(`${en_vi}.partNumber`),
			dataIndex: "eqpartNo"
		},
		{
			title: t(`${en_vi}.name`),
			dataIndex: "eqpartName"
		},
		{
			title: t(`${en_vi}.type`),
			dataIndex: "eqpartType"
		},
		{
			title: t(`${en_vi}.price`),
			dataIndex: "eqpartPrice"
		},
		{
			title: t(`${en_vi}.description`),
			dataIndex: "qepartDesc"
		},
		{
			title: t(`${en_vi}.quantity`),
			dataIndex: "number",
			editable: true,
			type: "InputNumber"
		},
		{
			title: t(`${en_vi}.batchNumber`),
			dataIndex: "batchNo"
		},
		{
			title: t(`${en_vi}.productionTime`),
			dataIndex: "produceDt",
			render: (produceDt: any) => formatDate(new Date(produceDt))
		},
		{
			title: t(`${en_vi}.warrantyStartTime`),
			dataIndex: "startDt",
			render: (startDt: any) => formatDate(new Date(startDt))
		},
		{
			title: t(`${en_vi}.warrantyEndTime`),
			dataIndex: "endDt",
			render: (endDt: any) => formatDate(new Date(endDt))
		}
	];
	const [registerTable, { submit: tableSubmit }] = useProTable({
		api: productApi.eqpartsPage,
		key: "eqpartId",
		type: "Checkbox",
		defaultColumns,
		dataSource,
		default: {
			number: 1
		}
	});
	const submit = async () => {
		let values = await tableSubmit();
		let partList: any = [];
		values.forEach((item: any) => {
			partList = [
				...partList,
				{
					eqpartId: item.eqpartId,
					eqpartQty: item.number
				}
			];
		});
		const res = await productApi.eqpPartsCreate({
			eqpId,
			partList
		});
		if (!res) message.error(t("kevin_ui.addFailed"));
		message.success(t("kevin_ui.addedSuccessfully"));
		setPageFn("1");
	};
	const getEqpart = async () => {
		const res = await productApi.eqpartsPage({});
		setDataSource(res.list);
	};
	useEffect(() => {
		getEqpart();
	}, []);
	return (
		<div className="partAdd">
			<BasicProTable register={registerTable}></BasicProTable>
			<div className="button">
				<Button onClick={submit} type="primary" block>
					{t(`Hospitalization.Hospitalization.depositHistory.submit`)}
				</Button>
			</div>
		</div>
	);
};

export default index;
